//
//  CZWelcomeViewControl.swift
//  weibo
//
//  Created by Mac on 16/5/16.
//  Copyright © 2016年 fengzhixiang. All rights reserved.
//

import UIKit
import SnapKit

class CZWelcomeViewControl: UIViewController {

    //view加载完成时执行方法
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
    }

    //view即将显示
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        //用户看到的界面的时候调用动画
        moveAnimation()

    }

    private func moveAnimation(){
        //弹簧动画
        // usingSpringWithDamping: 弹簧的明显程度 0 - 1
        // initialSpringVelocity: 初始速度
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.7, initialSpringVelocity: 9, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                //设置动画,snp_updateConstraints更新约束
                self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                    //更新底部约束
                    make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
                })
                //重新设置frame
                self.view.layoutIfNeeded()
            }) { (_) -> Void in

              UIView.animateWithDuration(2, animations: { () -> Void in
                self.messageLabel.hidden = false
                }, completion: { (_) -> Void in
                    //切换到MainViewController
                    let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
                    appDelegate.switchRootViewController(CZMainController())
              })
                
                CZPrint(items: "动画完成")

        }

    }


    private func prepareUI(){
        //布局ui
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)

        // 背景
        // bkgView: 要添加约束的view
        // snp_makeConstraints: 要添加约束
        // make: 要添加约束的view
        bkgView.snp_makeConstraints { (make) -> Void in
            //填充父控件
            make.edges.equalTo(self.view)
        }

        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }

        //消息label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(15)
            make.centerX.equalTo(iconView)
            self.messageLabel.hidden = true
        }




    }





    // MARK: - 懒加载
    ///背景
    private lazy var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    ///头像
    private lazy var iconView: UIImageView = {
    let imageView =  UIImageView(image: UIImage(named: "avatar_default_big"))
        //圆角处理
        imageView.layer.cornerRadius =
        max(imageView.bounds.height, imageView.bounds.width) * 0.5
        //裁边处理
        imageView.layer.masksToBounds = true

        return imageView
    }()


    ///消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        //设置label
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        return label
    }()



















}








